home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / asm_msc1.arc / EX58.ASM < prev    next >
Assembly Source File  |  1988-11-20  |  3KB  |  97 lines

  1. TITLE  Delete Element From Ordered List (EX58.ASM)
  2.           PAGE      ,132
  3. DATA      SEGMENT   PARA 'DATA'
  4. START_ADDR  DW      ?
  5. DATA      ENDS
  6. OUR_CODE  SEGMENT   PARA 'CODE'
  7.       PUBLIC    DEL_OL
  8. DEL_OL    PROC      FAR
  9.           ASSUME    CS:OUR_CODE,DS:DATA
  10.           PUSH      DS             ;Save caller's registers
  11.       PUSH        CX
  12.       PUSH        SI             
  13.       PUSH        BX
  14.       MOV       CX,SEG DATA         ;Initialize DS
  15.       MOV       DS,CX
  16.       CALL        B_SEARCH         ;Is the value in the list?
  17.       JC        ADIOS         ; If not, exit
  18.       MOV        CX,ES:[DI]         ; If so, find addr. of last element
  19.       SHL        CX,1
  20.       ADD        CX,DI         ; and put it in CX
  21.       CMP        CX,SI         ;Is the last el. to be deleted?
  22.       JE        CNT_M1         ; Yes.  Go decrement el. count
  23.       SUB        CX,SI         ; No.  Calculate move count
  24.       SHR        CX,1
  25. MOVEM:    MOV       BX,ES:[SI+2]     ;Move one element up in list
  26.       MOV        ES:[SI],BX
  27.       ADD        SI,2         ;Point to next element
  28.       LOOP        MOVEM         ;Repeat until all are moved
  29. CNT_M1:   DEC        WORD PTR ES:[DI]  ;Decrease element count by 1
  30. ADIOS:      POP        BX             ;Restore registers
  31.       POP        SI    
  32.       POP        CX
  33.       POP        DS
  34.       RET                 ; and exit
  35. DEL_OL    ENDP
  36. ;
  37. ;  This is the B_SEARCH procedure from Example 5-6.
  38. ;
  39. B_SEARCH  PROC
  40.       CMP        AX,ES:[DI+2]     ;Search value < or = first el.?
  41.       JA        CHK_LAST         ; No.  Go check last element
  42.       LEA        SI,ES:[DI+2]     ; Yes.  Fetch addr. of first el.
  43.       JE        EXIT_1ST         ;If value = 1st element, exit
  44.       STC                 ;If value < 1st element, set CF
  45. EXIT_1ST:  RET
  46. CHK_LAST: MOV        SI,ES:[DI]         ;Point to last element
  47.       SHL        SI,1
  48.       ADD        SI,DI
  49.       CMP        AX,ES:[SI]         ;Search value > or = last el.?
  50.       JB        SEARCH           ; No.  Go search list
  51.       JE        EXIT_LAST        ; Yes.  Exit if value = last el.
  52.       STC                 ;If value > last element, set CF
  53. EXIT_LAST: RET                       ;    and then exit
  54. ;
  55. ;  Search for value within the list.
  56. ;
  57. SEARCH:  MOV        START_ADDR,DI    ;Save starting address in memory
  58.       MOV        SI,ES:[DI]         ;Fetch index
  59. EVEN_IDX: TEST        SI,1         ;Force index to an even value
  60.       JZ        ADD_IDX
  61.       INC        SI
  62. ADD_IDX:  ADD        DI,SI         ;Calculate next search address
  63. COMPARE:  CMP        AX,ES:[DI]         ;Search value found?
  64.       JE        ALL_DONE         ; If so, exit
  65.       JA        HIGHER         ; Otherwise, find correct half
  66. ;
  67. ;  These instructions are executed if the search value is lower
  68. ;  in the list.
  69. ;
  70.       CMP        SI,2         ;Index = 2?
  71.       JNE        IDX_OK
  72. NO_MATCH: STC                 ; If so, set CF
  73.       JE        ALL_DONE         ;  and exit
  74. IDX_OK:   SHR        SI,1         ; If not, divide index by 2
  75.       TEST        SI,1         ;Force index to an even value
  76.       JZ        SUB_IDX
  77.       INC        SI
  78. SUB_IDX:  SUB        DI,SI         ;Calculate next address
  79.       JMP        SHORT COMPARE    ;Go check this element
  80. ;
  81. ;  These instructions are executed if the search value is higher
  82. ;  in the list.
  83. ;
  84. HIGHER:   CMP        SI,2         ;Index = 2?
  85.       JE        NO_MATCH         ; If so, go set CF and exit
  86.              SHR        SI,1         ; If not, divide index by 2
  87.       JMP        SHORT EVEN_IDX   ;Go check next element
  88. ;
  89. ;  Following are exit instructions.
  90. ;
  91. ALL_DONE: MOV       SI,DI         ;Move compare address into SI
  92.             MOV        DI,START_ADDR    ;Restore starting address
  93.            RET                 ; and exit
  94. B_SEARCH  ENDP
  95. OUR_CODE  ENDS
  96.          END       DEL_OL
  97.